
const { ccclass, property } = cc._decorator;

/**
 * 记录本脚本所在节点的子节点，一般放于scene, prefab的根节点
 */
@ccclass
export default class ButtonRecorder extends cc.Component {

    static readonly maxRecordCount = 20
    static buttonClickRecord = []

    start() {
        // let node = cc.Canvas.instance.node
        let buttonList = this.node.getComponentsInChildren(cc.Button)

        buttonList.forEach((btn: cc.Button) => {
            var clickEventHandler = new cc.Component.EventHandler();
            clickEventHandler.target = this.node; // 这个 node 节点是你的事件处理代码组件所属的节点
            clickEventHandler.component = "ButtonRecorder";// 这个是代码文件名
            clickEventHandler.handler = "callback";
            clickEventHandler.customEventData = "";
            btn.clickEvents.push(clickEventHandler);
        })
    }

    callback(e: cc.Event.EventTouch) {
        let btnNode = e.target as cc.Node
        if (ButtonRecorder.buttonClickRecord.length >= ButtonRecorder.maxRecordCount)
            ButtonRecorder.buttonClickRecord.pop();
        ButtonRecorder.buttonClickRecord.unshift(cc.director.getScene().name + ":" + btnNode.name)
        cc.log("当前按钮记录:", ButtonRecorder.buttonClickRecord.join(";"))
    }

    static getButtonRecordString() {
        return ButtonRecorder.buttonClickRecord.join(";")
    }
}
